package com.gxa.modules.sys.service.impl;

import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Query;
import com.gxa.modules.sys.dto.CouponAddDto;
import com.gxa.modules.sys.dto.CouponQueryDto;
import com.gxa.modules.sys.entity.Coupon;
import com.gxa.modules.sys.mapper.CouponMapper;
import com.gxa.modules.sys.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public PageUtils queryCoupon(Map<String, Object> params) {
        PageUtils pageUtils = new PageUtils(new Query<CouponQueryDto>().getPage(params));
        List<CouponQueryDto> couponList = this.couponMapper.queryCoupon(params);
        Integer count = this.couponMapper.count();
        pageUtils.setTotalCount(count);
        for(int i=0;i<couponList.size();i++){
            java.sql.Date sqlDate = new java.sql.Date(couponList.get(i).getExpireTime().getTime());
            couponList.get(i).setExpireTime(sqlDate);

        }
        pageUtils.setList(couponList);
        return pageUtils;
    }

    @Override
    public void addCoupon(CouponAddDto couponAddDto) {
        this.couponMapper.addCoupon(couponAddDto);
    }

    @Override
    public void deleteCoupon(List<Integer> id) {
        this.couponMapper.deleteCoupon(id);
    }

    @Override
    public void updatePrice(Coupon coupon) {
        this.couponMapper.updatePrice(coupon);
    }

    @Override
    public void updateSell(List<Coupon> couponList) {

        for(int i =0;i<couponList.size();i++){
            Coupon coupon = couponList.get(i);
            this.couponMapper.updateSell(coupon.getId(),coupon.getState());
        }

//        this.couponMapper.updateSell(couponList);
    }

    @Override
    public void updateHaltSales(List<Coupon> couponList) {
        for(int i = 0;i<couponList.size();i++){
            Coupon coupon = couponList.get(i);
            this.couponMapper.updateHaltSales(coupon.getId(),coupon.getState());
        }

    }
}
